$(document).ready(function() {
	
	var nom = document.forms["SIGNUPFORM"].NOM;					
	var prenom = document.forms["SIGNUPFORM"].PRENOM;			
	var mail = document.forms["SIGNUPFORM"].ADRESSEMAIL;
	var pseudo = document.forms["SIGNUPFORM"].PSEUDO;
	var password1 = document.forms["SIGNUPFORM"].PASSWORD;
	var password2 = document.forms["SIGNUPFORM"].PASSWORD2;
	var annee = document.forms["SIGNUPFORM"].ANNEE;
	var mois = document.forms["SIGNUPFORM"].MOIS;
	var jour = document.forms["SIGNUPFORM"].JOUR;
	var promo = document.forms["SIGNUPFORM"].PROMOTION;
	var formation = document.forms["SIGNUPFORM"].FORMATION;
	
	var nomOK=false, prenomOK=false, mailOK=false, pseudoOK=false, pass1OK=false, pass2OK=false, dateOK=false;

	
	function isMinimumCharactersEntered(value) {
		if(value.length>=2)
			return true;
		else
			return false;
	}
	
	function isGoodPassword(value) {
		if(value.length>6)
			return true;
		else
			return false;
	}
	
	function isPasswordOK(pass1, pass2) {
		if(pass1!=pass2)
			return false;
		else
			return true;
	}
	
	function isValideMail(adressemail) {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(filter.test(adressemail))
			return true;
		else
			return false;
	}
	
	function isValideDate() {
		if(annee.value!='default' && mois.value!='default' && jour.value!='default') {
			dateOK=true;
			
		}
		else {
			dateOK=false;
			$('select[name=ANNEE]').parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Date non valide');
		}
	}
	
	$('input').focus(function() {
		$(this).css('box-shadow','');
	});
	
	
	// VALIDATION DU NOM ET DU PRENOM DU NOUVEL UTILISATEUR
	$('input[name=NOM], input[name=PRENOM]').blur(function() {
		var obj = $(this);
		if(isMinimumCharactersEntered(obj.attr('value'))) {
			obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
			obj.css('box-shadow','0px 0px 1px 2px #2cd659');
			if(obj.attr('name')=='NOM')
				nomOK=true;
			else
				prenomOK=true;
		} 
		else {
			obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>2 caractères minimum');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			if(obj.attr('name')=='NOM')
				nomOK=false;
			else
				prenomOK=false;
		}
	});
	
	// VALIDATION DU PSEUDO : ON VERIFIE SI IL EST VALIDE ET SI IL N'EXISTE PAS DANS LA BASE DE DONNEES
	$('input[name=PSEUDO]').blur(function() {
		var obj = $(this);
		if(isMinimumCharactersEntered(pseudo.value)) {
			$.ajax( {
				type: "GET",
				url: "index.php?control=user&action=checkpseudo",
				data: "pseudo="+$(this).attr('value'),
				beforeSend: function() {
					obj.parent().parent().children('.error').html('<img src="images/preloader.gif" alt="" title=""/>');
				},
				success: function(msg){
					if(msg=="ok") {
						obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
						obj.css('box-shadow','0px 0px 1px 2px #2cd659');
						pseudoOK=true;
					}
					else {
						obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Déja pris');
						obj.css('box-shadow','0px 0px 1px 2px #fb9000');
						pseudoOK=false;
					}
				}
			});
		} else {
			obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>2 caractères minimum');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pseudoOK=false;
		}
	});
	
	
	// VALIDATION DE L'ADRESSE EMAIL : ON VERIFIE SI ELLE EST VALIDE ET SI ELLE N'EXISTE PAS DANS LA BASE DE DONNEES
	$('input[name=ADRESSEMAIL]').blur(function() {
		var obj = $(this);
		if(isValideMail(mail.value)) {
			$.ajax( {
				type: "GET",
				url: "index.php?control=user&action=checkmail",
				data: "mail="+$(this).attr('value'),
				beforeSend: function() {
					obj.parent().parent().children('.error').html('<img src="images/preloader.gif"/>');
				},
				success: function(msg){
					if(msg=="ok") {
						obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
						obj.css('box-shadow','0px 0px 1px 2px #2cd659');
						mailOK=true;
					}
					else {
						obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Déja pris');
						obj.css('box-shadow','0px 0px 1px 2px #fb9000');
						mailOK=false;
					}
				}
			});
		}
		else {
			obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Adresse invalide');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			mailOK=false;
		}
	});
	
	// VALIDATION DU PASSWORD : 6 CARACTERES MINIMUM
	$('input[name=PASSWORD]').blur(function() {
		var obj = $(this);
		if(isGoodPassword(obj.attr('value'))) {
			obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
			obj.css('box-shadow','0px 0px 1px 2px #2cd659');
			pass1OK=true;
		} 
		else {
			obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>6 caractères minimum');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pass1OK=false;
		}
	});
	
	// VALIDATION DE LA REFRAPPE DU MOT DE PASSE
	$('input[name=PASSWORD2]').blur(function() {
		var obj = $(this);
		if(isPasswordOK(password1.value, password2.value)) {
			obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
			obj.css('box-shadow','0px 0px 1px 2px #2cd659');
			pass2OK=true;
		} 
		else {
			obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Mots de passe différents');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pass2OK=false;
		}
	});
	
	$('form[name=SIGNUPFORM]').submit(function() {
		isValideDate();
		return (nomOK && prenomOK && mailOK && pseudoOK && pass1OK && pass2OK && dateOK);
	});
});

